using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Configuration;

namespace DAL
{
   public class tbHoaDonDAL
    {
               private SqlConnection cn = null;
        public tbHoaDonDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

       public tbHoaDonDAL(tbHoaDonDAL tan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public int Insert(tbHoaDon th)
        {
            SqlCommand cm = new SqlCommand("tbHoaDon_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@khachhang", th.khachhang);
            cm.Parameters.AddWithValue("@ngaydathang", th.ngaydathang);
            cm.Parameters.AddWithValue("@ngaygiaohang", th.ngaygiaohang);
            cm.Parameters.AddWithValue("@trigia", th.trigia);
            cm.Parameters.AddWithValue("@phuongthucthanhtoan", th.phuongthucthanhtoan);
            cm.Parameters.AddWithValue("@tinhtranggiaohang", th.tinhtranggiaohang);
            cm.Parameters.AddWithValue("@nhanviengiaohang", th.nhanviengiaohang);
            cm.Parameters.AddWithValue("@diachinhanhang", th.diachinhanhang);
            cm.Parameters.AddWithValue("@dienthoai", th.dienthoai);
            cm.ExecuteNonQuery();
            SqlDataReader dr = cm.ExecuteReader(CommandBehavior.SingleRow);
            dr.Read();
            return int.Parse(dr["mahd"].ToString());
        }
       public void UpdateTTGH(tbHoaDon th)
       {
           SqlCommand cm = new SqlCommand("UpdateTinhTrangGiaoHang", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@mahd", th.mahd);
           cm.Parameters.AddWithValue("@tinhtranggiaohang", th.tinhtranggiaohang);
           cm.ExecuteNonQuery();
       }
        public void Update(tbHoaDon th)
        {
            SqlCommand cm = new SqlCommand("tbHoaDon_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@mahd", th.mahd);
            cm.Parameters.AddWithValue("@khachhang", th.khachhang);
            cm.Parameters.AddWithValue("@ngaydathang", th.ngaydathang);
            cm.Parameters.AddWithValue("@ngaygiaohang", th.ngaygiaohang);
            cm.Parameters.AddWithValue("@trigia", th.trigia);
            cm.Parameters.AddWithValue("@phuongthucthanhtoan", th.phuongthucthanhtoan);
            cm.Parameters.AddWithValue("@tinhtranggiaohang", th.tinhtranggiaohang);
            cm.Parameters.AddWithValue("@nhanviengiaohang", th.nhanviengiaohang);
            cm.Parameters.AddWithValue("@diachinhanhang", th.diachinhanhang);
            cm.Parameters.AddWithValue("@dienthoai", th.dienthoai);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbHoaDon_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@makh", iD);
            cm.ExecuteNonQuery();
        }
        public tbHoaDonCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbHoaDon_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbHoaDonCollection hdCollection = new tbHoaDonCollection();

            while (dr.Read())
            {
                tbHoaDon th = new tbHoaDon();
                th.mahd = int.Parse(dr.GetValue(0).ToString());
                th.khachhang = int.Parse(dr.GetValue(1).ToString());
                th.ngaydathang = DateTime.Parse(dr.GetValue(2).ToString());
                th.ngaygiaohang = DateTime.Parse(dr.GetValue(3).ToString());
                th.trigia = float.Parse(dr.GetValue(4).ToString());
                th.phuongthucthanhtoan = int.Parse(dr.GetValue(5).ToString());
                th.tinhtranggiaohang = int.Parse(dr.GetValue(6).ToString());
                th.nhanviengiaohang = int.Parse(dr.GetValue(7).ToString());
                th.diachinhanhang = dr.GetValue(8).ToString();
                th.dienthoai = dr.GetValue(9).ToString();
                hdCollection.Add(th);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return hdCollection;
        }
    }
}
